package com.ins.common.utils;

import com.ins.common.enums.ErrEnum;
import lombok.Data;

/**
 * 响应数据
 *
 * @author ins
 * @since 2023-3-10
 */
@Data
public class Result<T> {

    /**
     * 编码 200表示成功，其他值表示失败
     */
    private int code = 200;

    /**
     * 请求状态
     */
    private boolean status = true;

    /**
     * 消息内容
     */
    private String msg = "success";

    /**
     * 响应数据
     */
    private T data;

    public static <T> Result<T> ok() {
        return new Result<>();
    }

    public static <T> Result<T> ok(int code, String msg) {
        Result<T> result = ok();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = ok();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> err() {
        Result<T> result = new Result<>();
        result.setStatus(false);
        result.setCode(500);
        result.setMsg("请求失败，请联系客服");
        return result;
    }

    public static <T> Result<T> err(String msg) {
        return err(500, msg);
    }

    public static <T> Result<T> err(ErrEnum errEnum) {
        return err(errEnum.getCode(), errEnum.getMsg());
    }

    public static <T> Result<T> err(int code, String msg) {
        Result<T> result = err();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> err(int code, String msg, T data) {
        Result<T> result = err(code, msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> err(ErrEnum errEnum, T data) {
        return err(errEnum.getCode(), errEnum.getMsg(), data);

    }
}
